﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DimensionAddIn
{
    /// <summary>
    /// excel的位置信息
    /// </summary>
    public class ExcelPos
    {
        public int Row { get; set; }
        public int Col { get; set; }

        public ExcelPos(int row, int col)
        {
            Row = row;
            Col = col;
        }

        /// <summary>
        /// 转换成excel中的位置描述
        /// 类似：AA11
        /// </summary>
        /// <returns></returns>
        public string ToXlsString()
        {
            return string.Format("{1}{0}", Row, ToAA(Col));
        }

        private string ToAA(int value)
        {
            string AA = string.Empty;
            int d = value / 26;
            int remainder = value % 26;

            if (d == 1 && remainder == 0)
            {
                AA = ToA(26) + AA;
                d = 0;
            }
            else
            {
                AA = ToA(remainder) + AA;
            }

            while (d > 0)
            {
                remainder = d % 26;
                d = d / 26;
                AA = ToA(remainder) + AA;
            }
            return AA;
        }

        private string ToA(int value)
        {
            if (value <= 0)
            {
                return string.Empty;
            }
            return new string((char)('A' + (value - 1)), 1);
        }
    }
}
